﻿using System.Collections.Generic;
using System.Windows;
using System;
using System.Web.Security;
using System.Threading;

using Acceron.University.Core.Addins;

namespace Acceron.University
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        AddinManager addManager;
        
        public MainWindow()
        {
            try
            {
                InitializeComponent();

                addManager = AddinManager.Instanse;
                addManager.LoadModules();

                int i = 0;
                foreach (var module in addManager.Modules)
                {
                    var btn = new System.Windows.Controls.Button()
                    {
                        Tag = i,
                        ToolTip = module.Name,
                        Content = new System.Windows.Controls.Image()
                        {
                            Source = module.Source,
                            Width = 25,
                            Height = 25
                        },
                        Background = System.Windows.Media.Brushes.Transparent,
                        BorderBrush = System.Windows.Media.Brushes.Transparent
                    };
                    btn.Click += new RoutedEventHandler(btn_Click);
                    modulesDockPanel.Children.Add(btn);

                    i++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "FATAL ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            holderPlace.Children.Clear();

            holderPlace.Children.Add(addManager.Modules[(int)((System.Windows.Controls.Button)sender).Tag].Control);
        }

        private bool SignIn(string username, string pass)
        {
            try
            {
                if (!System.Web.Security.Membership.ValidateUser(
                   username, pass))
                {
                    MessageBox.Show("Unable to authenticate.", "Not logged in", MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                    //this.Close();
                }
                else
                {
                    loginLbl.Content = "Hello, " + username;
                    return true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot loggin. " + ex.Message, "SIGN IN FAILED");
            }

            return false;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
          
            //if (SignIn(loginTxtbox.Text, passwordBox.Password))
            {
            //    holderPlace.Children.Clear();
            }
     
        }

    }
}
